home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fatted Calf
/
The Fatted Calf.iso
/
Applications
/
Graphics
/
Pyramid
/
Source
/
matrix.subproj
/
perspect.cc
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-15
|
3KB
|
141 lines
/* perspect.cc - matrix algebra definitions
* Copyright (C) 1993 Corona Design, Inc. All rights reserved.
*
* Abstract
* Common 3D perspective transformations. Provides an arbitrary
* view plane with a center of perspective 1 unit (in VIEW space)
* from the view plane.
*
* Reference: Foley & van Dam, Computer Graphics
* C callable.
*
* RCS path:
* $Source: /Users/pkron/Projects/voxel/Pyramid/matrix.subproj/RCS/perspect.cc,v $
* Modified: $Date: 93/09/15 12:34:01 $ by $Author: pkron $
* Current State: $State: Exp $ locked by $Locker: $
*/
#include "standard.h"
#include "matrix.h"
#include "perspect.h"
// create an identity matrix
MATRIX
allocIdentity()
{
MATRIX matrix = new struct _matrix;
*matrix = unitMatrix;
return( matrix);
}
void
freeMatrix( MATRIX matrix)
{
delete matrix;
}
// multiply matrix by point
void
map( MATRIX matrix, POINT *aPoint)
{
((VECTOR *)aPoint)->matrixMultiply( matrix);
}
// define the matrix such that
// the viewing transform is normal
// to the given vector "normal"
// and the vector "up" projects onto
// the positive Y-axis
void
orient( MATRIX matrix, POINT *normal, POINT *up)
{
VECTOR rX = unitVector;
VECTOR rY = unitVector;
VECTOR rZ = unitVector;
// compute axis unit vectors
rZ = *(VECTOR *)normal;
rZ.scalarMultiply( 1/rZ.vectorLength());
rX = *(VECTOR *)up;
rX.crossProduct( &rZ);
rX.scalarMultiply( 1/rX.vectorLength());
rY = rZ;
rY.crossProduct( &rX);
// now build rotation matrix
*matrix = unitMatrix;
*matrix->getRx() = rX;
*matrix->getRy() = rY;
*matrix->getRz() = rZ;
}
// project a point onto the viewplane
void
project( POINT *aPoint)
{
((VECTOR *)aPoint)->scalarMultiply( 1/(*aPoint)[2]);
}
void
scaleProjection( MATRIX transform, int width, int height)
{
// projection matrix
struct _matrix scale = unitMatrix;
// scale and translate origin
// to lower left
// (no translation needed for z)
scale.matrix[0][0] = width;
scale.matrix[1][1] = height;
transform->matrixMultiply( &scale);
}
void
translate( MATRIX matrix, POINT *to)
{
matrix->matrix[0][3] -= (*to)[0];
matrix->matrix[1][3] -= (*to)[1];
matrix->matrix[2][3] -= (*to)[2];
}
void
viewDistance( MATRIX matrix, float distance)
{
POINT COP;
struct _matrix Mper; // foreshortening transform
COP[0] = 0;
COP[1] = 0;
COP[2] = distance; // eye is this distance out
COP[3] = 1;
translate( matrix, &COP);
// account for distance foreshortening
Mper = unitMatrix;
Mper.matrix[2][2] = -1; // put eye on negative z axis
matrix->matrixMultiply( &Mper);
}
#ifdef _LOG
/*
* $Log: perspect.cc,v $
Revision 1.1 93/09/15 12:34:01 pkron
Created
*/
#endif